<template>
  <div>
    <div style="display: flex;justify-content: center">
      <!--      大图-->
      <el-image :src="showBigImg" style="width: 400px; height: 400px">
        <template #error>
          <div class="image-slot">
            <el-icon>
              <icon-picture/>
            </el-icon>
          </div>
        </template>
      </el-image>
    </div>
    <!--    小图-->
    <div style="display: flex;justify-content: space-between;margin-top: 20px">
      <el-icon style="width: 50px;height: 50px" @click="last">
        <ArrowLeftBold/>
      </el-icon>
      <div style="display: flex;justify-content: center;">
        <div v-for="item in showList" :key="item" :class="item===showBigImg ? 'is_show' : 'not_show' "
             @mouseover="show(item)" @click="show(item)" style="margin-left: 20px">
          <el-image :src="item" fit="fill" style="width: 50px;height: 50px"/>
        </div>
      </div>
      <el-icon @click="next" style="width: 50px;height: 50px">
        <ArrowRightBold/>
      </el-icon>
    </div>
  </div>
</template>
<script setup lang="ts">
import {ref, defineProps, watch,} from 'vue'

let props = defineProps({
  imgList: {
    type: Array as () => string[],
    default: () => []
  }
})
let showBigImg = ref(props.imgList[0])//页面初始化的时候显示第一个图片
let pageNum = ref(0)//当前展示图片处于多少页
let showList = ref(props.imgList.slice(0, 4))//初始化只展示前四个

function show(big) {
  showBigImg.value = big
}
//下一页
function last() {
//  通过判断showBigImg的值来判断当前处于元素
  if (Array.isArray(showList.value)) {
    //查找索引，判断是否是最后一个元素（注意此处是原数组）
    if (pageNum.value === 0) {
      return
    }
    pageNum.value--
  }
}
//上一页
function next() {
  if (Array.isArray(props.imgList)) {
    //计算最大的页码，向上取整
    let mixPage = Math.ceil(props.imgList.length / 4);
    if ((pageNum.value + 1) === mixPage) {
      //如果是最后一页，则不能翻页
      return
    }
    //如果不是最后一页，则翻页
    pageNum.value++
  } else {
    console.log("不是数组类型")
  }
}
watch(() => props.imgList, () => {
  //监听父组件传递数据的变化，修改展示的数据
  showBigImg.value=props.imgList[0]
  showList.value = props.imgList.slice(0, 4)
})
watch(pageNum, () => {
//  监听pageNum的值变换，修改showList的显示数据
  showList.value = props.imgList.slice(pageNum.value * 4, (pageNum.value + 1) * 4)
  showBigImg.value = showList.value[0]
})
</script>

<style scoped>
.not_show {
  width: 50px;
  height: 50px;
  border: 1px;
}

.is_show {
  width: 50px;
  height: 50px;
  border: solid 3px #098CC0;
}
</style>
